#include "autoupdata.h"
#include "ui_autoupdata.h"

autoUpdata::autoUpdata(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::autoUpdata)
{
    ui->setupUi(this);
    //setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);

    my_process = new QProcess();
    my_timer = new QTimer();
    connect(my_process,SIGNAL(readyRead()),this,SLOT(readCmdInformation()));
    connect( my_process, SIGNAL(finished(int)),this,SLOT(psFinished(int)));
    my_updataEnable = false;
    my_udiskEnable = false;
}

autoUpdata::~autoUpdata()
{
    delete ui;
}

void autoUpdata::setUpdatePath(QString path)
{
    QDir dir(path);
    my_udiskEnable = true;

    //qDebug() << path <<__FILE__<<__LINE__;
    ui->label_udiskPath->setText( "检测到有U盘插入");
    emit my_timer->singleShot(1000,this,SLOT(displayUdiskFileList()));
}


void autoUpdata::displayUdiskFileList()
{
    const QDir udir("/mnt/sda1");
    QStringList uDiskList = udir.entryList(QDir::NoDotAndDotDot  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);

    for( int i = 0; i < uDiskList.length(); ++i )
    {
        ui->plainTextEdit->appendPlainText( uDiskList[i] );
        ui->plainTextEdit->moveCursor(QTextCursor::End);
    }
}


void autoUpdata::on_pushButton_updata_clicked()
{
    my_updataEnable = true;
    ui->label_udiskPath->setText( "系统正在升级，请不要拔出U盘和电源!");
    ui->plainTextEdit->appendPlainText("updata......");
    ui->plainTextEdit->moveCursor(QTextCursor::End);

    my_process->start("/mnt/sda1/userUp.sh");
}



//! 读取命令窗口的信息，显示在自己的窗口上
void autoUpdata::readCmdInformation()
{
    my_upstr  =  my_process->readAllStandardOutput();

    ui->plainTextEdit->appendPlainText( my_upstr );
    ui->plainTextEdit->moveCursor(QTextCursor::End);
}


void autoUpdata::psFinished(int nu)
{
    ui->label_udiskPath->setText("请拔出U盘，准备重启系统" );
    ui->plainTextEdit->appendPlainText("复制文件完成。");
    ui->plainTextEdit->moveCursor(QTextCursor::End);
}

void autoUpdata::reboot()
{
     QProcess::execute("reboot");
}

void autoUpdata::on_pushButton_exit_clicked()
{
    my_updataEnable = false;
    this->disConnectDisk();
    this->close();
}


void autoUpdata::disConnectDisk()
{
    ui->pushButton_updata->setText("升级");
    my_upstr.clear();
    ui->plainTextEdit->clear();
}

void autoUpdata::rebootSys()
{
    if( my_updataEnable )
    {
        ui->label_udiskPath->setText("U盘已经拔出，5秒后重启系统，请勿切断电源。" );
        emit my_timer->singleShot(5000,this,SLOT(reboot()));
    }
}


void autoUpdata::keyPressEvent(QKeyEvent *e)
{
    switch( e->key() )
    {
        case Qt::Key_Q:
            on_pushButton_updata_clicked();
            break;
        case Qt::Key_R:
            on_pushButton_exit_clicked();
            break;
        case Qt::Key_Shift:
            emit this->copyFile();
            break;
        default:
            break;
    }
}

void autoUpdata::progress(QString str)
{
    if( my_udiskEnable )
    {
        ui->plainTextEdit->appendPlainText( str);
        ui->plainTextEdit->moveCursor(QTextCursor::End);
    }

}
